package com.training.controller;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.training.common.Result;
import com.training.common.TrainingPage;
import com.training.common.model.Class;
import com.training.common.model.User;
import com.training.service.ClassService;
import com.training.service.ClassServiceImp;

import java.util.Date;
import java.util.List;

public class ClassController extends Controller {

    @Inject(ClassServiceImp.class)
    private ClassService classService;

    public void index(){
        render("/views/class/index.html");
    }

    public void getFuClassList(){
        List<Class> fuClassList = classService.getFuClassList();
        Result result = new Result();
        result.setData(fuClassList);
        renderJson(result);
    }

    public void showAddFuClass(){
        render("/views/class/addFuClass.html");
    }

    public void addFuClass(){
        String name = getPara("name");
        User user = getSessionAttr("trainingsession");
        Class aClass = new Class();
        aClass.setClassName(name);
        aClass.setUserId(user.getID());
        aClass.setClassId(0);
        aClass.setCreatetime(new Date());
        boolean boo = aClass.save();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    public void showEditFuClass(){
        String menuId = getPara("menuId");
        setAttr("id",menuId);
        render("/views/class/editFuClass.html");
    }

    public void getFuClass(){
        String id = getPara("menuId");
        Class aClass = Class.dao.findById(id);
        Result result = new Result();
        result.setData(aClass);
        renderJson(result);
    }

    public void editFuClass(){
        String id = getPara("id");
        String name = getPara("class_name");
        Result result = new Result();
        Class aClass = Class.dao.findById(id);
        aClass.setClassName(name);
        boolean boo = aClass.update();
        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("更新成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("更新失败！");
        }
        renderJson(result);
    }

    public void removeFuClass(){
        String menuId = getPara("menuId");
        Result result = new Result();
        boolean boo = Class.dao.deleteById(menuId);
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }

    public void getClassPage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String menuId = getPara("menuId");
        Page<Class> classList = classService.getClassPage(pageNumber,pageSize,menuId);
        Result result = new Result();
        result.setData(new TrainingPage(classList));
        renderJson(result);
    }

    public void showAddClass(){
        setAttr("id",getPara("menuId"));
        render("/views/class/add.html");
    }

    public void addClass(){
        int id = getParaToInt("id");
        String name = getPara("name");
        User user = getSessionAttr("trainingsession");
        Class aclass = new Class();
        aclass.setClassName(name);
        aclass.setUserId(user.getID());
        aclass.setClassId(id);
        aclass.setCreatetime(new Date());
        boolean boo = aclass.save();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    public void removeClass(){
        String id = getPara("id");
        boolean boo = Class.dao.deleteById(id);
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }

    public void showEditClass(){
        setAttr("id",getPara("id"));
        render("/views/class/edit.html");
    }

    public void getClassa(){
        String id = getPara("id");
        Class aClass = Class.dao.findById(id);
        Result result = new Result();
        result.setData(aClass);
        renderJson(result);
    }

    public void editClass(){
        String id = getPara("id");
        String name = getPara("class_name");
        Result result = new Result();
        Class aClass = Class.dao.findById(id);
        aClass.setClassName(name);
        boolean boo = aClass.update();
        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("更新成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("更新失败！");
        }
        renderJson(result);
    }

}
